//
//  WPPopView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/3/22.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPPopView.h"

#define DURATION 0.5f

@interface WPPopView ()<UITextFieldDelegate>
@property (nonatomic, strong) NSString * title;
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UIButton * cancelbtn;
@property (nonatomic , strong) UIView *mask;
@end
@implementation WPPopView

- (instancetype)init {
    if ([super init]) {
        _title = @"";
        [self initializeBasicAppearance];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
    if ([super initWithFrame:frame]) {
        _title = title;
        [self initializeBasicAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeBasicAppearance {
    if (self.frame.size.height == 0) {
        self.bounds = CGRectMake(0, 0, SCREEN_SIZE.width*0.8, SCREEN_SIZE.height * 0.7);
        self.center = [UIApplication sharedApplication].keyWindow.center;
    }
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    [self addSubview:self.titleLabel];
    [self addSubview:self.cancelbtn];
}
#pragma mark - responds event
- (void)respondsToCancelBtn {
    [self hide];
}
- (void)drawRect:(CGRect)rect {
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, CGRectGetHeight(self.titleLabel.frame))];
    [path addLineToPoint:CGPointMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.titleLabel.frame))];
    path.lineWidth = 2;
    UIColor *color = COLOR_RGB(234, 27, 47, 1);
    [color setStroke];
    [path stroke];
    
}
#pragma mark 显示\隐藏动画
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self.mask];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 0.0;
    [UIView animateWithDuration:DURATION animations:^{
        self.alpha = 1.0;
    } completion:nil];
}

- (void)hide {
    [UIView animateWithDuration:DURATION animations:^{
        self.mask.alpha = 0.0;
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.mask removeFromSuperview];
    }];
}
#pragma mark - getter
- (UIView *)mask {
    if (!_mask) {
        _mask = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
            [view addGestureRecognizer:tap];
            view;
        });
    }
    return _mask;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(25*DHFlexibleHorizontalMutiplier(), 0, CGRectGetMaxX(self.bounds)-40*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier())];
            label.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            label.textColor = COLOR_RGB(234, 63, 64, 1);
            label.text = _title;
            label;
        });
    }
    return _titleLabel;
}
- (UIButton *)cancelbtn {
    if (!_cancelbtn) {
        _cancelbtn = ({
            UIButton * btn =[ UIButton buttonWithType:UIButtonTypeCustom];
            btn.bounds = CGRectMake(0, 0, 22*DHFlexibleHorizontalMutiplier(), 22*DHFlexibleHorizontalMutiplier());
            btn.center = CGPointMake(CGRectGetMaxX(self.bounds)-21*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier());
            [btn setImage:[UIImage imageNamed:@"gban"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToCancelBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _cancelbtn;
}
@end
